package com.hanter.demo.raddemo

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.hanter.android.radui.mvp.MvpTitleBarActivity
import com.hanter.android.radwidget.BannerView
import com.hanter.android.radwidget.viewpager.transformer.DepthPageTransformer
import java.util.ArrayList

class BannerActivity : MvpTitleBarActivity<BannerContract.View, BannerContract.Presenter>(),
    BannerContract.View {

    private val images = ArrayList<String>()
    private val titles = ArrayList<String>()

    override fun getLayout(): Int = R.layout.activity_banner

    override fun createPresenter() = BannerPresenter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231045958&di=08777f2beb345a39899e609096842a91&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201312%2F27%2F20131227232853_fJCmr.jpeg")
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231056826&di=22388264ffd2c76b4b3068b2b4c653e8&imgtype=0&src=http%3A%2F%2Fpic2.52pk.com%2Ffiles%2F120807%2F1283568_102511_4.jpg")
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231060174&di=bc3c96b2c21867cbd84b8f32f9127651&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201503%2F05%2F20150305213902_TP2ja.jpeg")
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231067360&di=6db2392eaa536655c69d341cc97aa38f&imgtype=0&src=http%3A%2F%2Fpic15.nipic.com%2F20110617%2F6901455_014602752117_2.jpg")
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231112745&di=1ac1ff4ee3c7ce2ce8dc609005a3bd83&imgtype=0&src=http%3A%2F%2Fdmimg.5054399.com%2Fallimg%2Foptuji%2Fcl3%2F040.jpg")

        titles.add("1")
        titles.add("2")
        titles.add("3")
        titles.add("4")
        titles.add("5")

        val bannerView = findViewById<BannerView>(R.id.banner)
        bannerView.setBannerLoader(GlideImageLoader())
            .setImages(images)
            .setBannerTitles(titles)
            .setIndicatorStyle(BannerView.INDICATOR_CIRCLE_TITLE)
            .setPageTransformer(false, DepthPageTransformer())
            .start()

        bannerView.setOnItemClickListener { _, position ->
            Toast.makeText(
                this@BannerActivity,
                "click banner: $position",
                Toast.LENGTH_SHORT
            ).show()
        }

    }

    inner class GlideImageLoader : BannerView.BannerLoader {

        override fun displayImage(
            context: Context,
            path: Any?,
            placeHolder: Drawable?,
            imageView: ImageView
        ) {
            val opts = RequestOptions()
                .placeholder(placeHolder)
                .centerCrop()

            Glide.with(context)
                .load(path)
                .apply(opts)
                .into(imageView)
        }

        override fun createImageView(container: ViewGroup, position: Int): ImageView {
            return LayoutInflater.from(container.context).inflate(
                R.layout.item_banner_image,
                container,
                false
            ) as ImageView
        }
    }

}
